package com.boss.salary.controller;

import com.boss.api.dto.ApiResultDTO;
import com.boss.common.base.BasicinfoBaseController;
import com.boss.common.entity.PageDTO;
import com.boss.common.util.PageUtils;
import com.boss.framework.dto.PaginationDTO;
import com.boss.framework.exception.AppRuntimeException;
import com.boss.salary.entity.OperrateLogs;
import com.boss.salary.entity.PayeeAcct;
import com.boss.salary.service.IOperrateLogsService;
import com.boss.salary.service.IPayeeAcctService;
import com.boss.sys.ca.user.dto.UserDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.boss.salary.controller.ProvinceController.getOutputStream;

/**
 * <br>Title: 收款账户
 * <br>Description:
 * <br>Author:
 * <br>Date:
 */
@RestController
@RequestMapping("/payeeAcctController")
public class PayeeAcctController extends BasicinfoBaseController {

    /**
     * 引入日志框架常量
     */
    private final static Logger logger = LoggerFactory.getLogger(HomeController.class);

    @Autowired
    private IPayeeAcctService iPayeeAcctService;
    @Autowired
    private IOperrateLogsService iOperrateLogsService;



    /**
     * 查询收款账户信息
     * @param tokenid
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/getAccountInfo")
    public ApiResultDTO getAccountInfo(@RequestHeader("X-Token") String tokenid,
                                       @RequestParam("page") String page,
                                       @RequestParam("limit") String limit,
                                       @RequestParam(value = "payeeAcctName", required = false) String payeeAcctName,
                                       @RequestParam(value = "payeeAcctNo", required = false) String payeeAcctNo,
                                       @RequestParam(value = "payeeAcctCode", required = false) String payeeAcctCode,
                                       @RequestParam(value = "payeeAcctBankName", required = false) String payeeAcctBankName) {
        try {
            UserDTO userDTO = getUser(tokenid);
            PaginationDTO paginationDTO = PageUtils.buildPaginationDTOPage(page, limit);
            Map<String, Object> queryParam = new HashMap<>(2);
            queryParam.put("mofDivCode", userDTO.getProvince());
            queryParam.put("year", userDTO.getYear());
            queryParam.put("payeeAcctName", payeeAcctName);
            queryParam.put("payeeAcctNo", payeeAcctNo);
            queryParam.put("payeeAcctCode", payeeAcctCode);
            queryParam.put("payeeAcctBankName", payeeAcctBankName);
            PageDTO<PayeeAcct> pageDTO = iPayeeAcctService.getAccountInfo(paginationDTO, queryParam);
            return ApiResultDTO.success(pageDTO);
        } catch (Exception e) {
            logger.error("查询收款账户信息失败", e.getMessage());
            return ApiResultDTO.error(ERROR_RSCODE, "查询收款账户信息失败");
        }
    }

    /**
     * 校验编码或者账户重复
     * @param tokenid
     * @param payeeAcctNo
     * @param payeeAcctCode
     * @return
     */
    @GetMapping("/checkRepeat")
    public ApiResultDTO checkRepeat(@RequestHeader("X-Token") String tokenid,
                                    @RequestParam("payeeAcctNo") String payeeAcctNo,
                                    @RequestParam("payeeAcctCode") String payeeAcctCode,
                                    @RequestParam("id") String id) {
        try {
            UserDTO userDTO = getUser(tokenid);
            Map<String, Object> queryParam = new HashMap<>(2);
            queryParam.put("mofDivCode", userDTO.getProvince());
            queryParam.put("payeeAcctNo", payeeAcctNo);
            queryParam.put("payeeAcctCode", payeeAcctCode);
            queryParam.put("id", id);
            String result = iPayeeAcctService.checkRepeat(queryParam);
            return ApiResultDTO.success(result);
        } catch (Exception e) {
            logger.error("查询收款账户信息失败", e.getMessage());
            return ApiResultDTO.error(ERROR_RSCODE, "查询收款账户信息失败");
        }

    }

    /**
     * 保存收款账户信息
     * @param tokenid
     * @param payeeAcct
     * @return
     */
    @PostMapping("/saveAccount")
    public ApiResultDTO saveAccount(@RequestHeader("X-Token") String tokenid,
                                       @RequestBody PayeeAcct payeeAcct) {
        try {
            UserDTO userDTO = getUser(tokenid);
            //添加操作日志
            OperrateLogs operrateLogs= new OperrateLogs();
            operrateLogs.setMofDivCode(userDTO.getProvince());
            operrateLogs.setTokenId(tokenid);
            operrateLogs.setUserAgencyId(userDTO.getAgency());
            operrateLogs.setUeserName(userDTO.getName());
            operrateLogs.setOperateName("财务审核--收款户配置--新增");
            iOperrateLogsService.saveOperrateLogs(operrateLogs);


            payeeAcct.setMofDivCode(userDTO.getProvince());
            payeeAcct.setYear(String.valueOf(userDTO.getYear()));
            iPayeeAcctService.saveAccount(payeeAcct);
            return ApiResultDTO.success();
        } catch (Exception e) {
            logger.error("保存收款账户信息失败", e.getMessage());
            return ApiResultDTO.error(ERROR_RSCODE, "保存收款账户信息失败");
        }
    }

    /**
     * 修改收款账户信息
     * @param tokenid
     * @param payeeAcct
     * @return
     */
    @PostMapping("/updateAccount")
    public ApiResultDTO updateAccount(@RequestHeader("X-Token") String tokenid,
                                      @RequestBody PayeeAcct payeeAcct) {
        try {
//            UserDTO userDto=getUser(tokenid);
//            //添加操作日志
//            OperrateLogs operrateLogs= new OperrateLogs();
//            operrateLogs.setMofDivCode(userDto.getProvince());
//            operrateLogs.setTokenId(tokenid);
//            operrateLogs.setUserAgencyId(userDto.getAgency());
//            operrateLogs.setUeserName(userDto.getName());
//
//            operrateLogs.setOperateName("财务审核--收款户配置--新增");
//            iOperrateLogsService.saveOperrateLogs(operrateLogs);

            iPayeeAcctService.updateAccount(payeeAcct);
            return ApiResultDTO.success();
        } catch (AppRuntimeException e) {
            logger.error("修改收款账户信息失败", e.getMessage());
            return ApiResultDTO.error(ERROR_RSCODE, "修改收款账户信息失败");
        }
    }

    /**
     * 删除收款账户信息
     * @param tokenid
     * @param accountId
     * @return
     */
    @PostMapping("/deleteAccount")
    public ApiResultDTO deleteAccount(@RequestHeader("X-Token") String tokenid,
                                      @RequestBody List<String> accountId) {
        try {
//            UserDTO userDTO = getUser(tokenid);
//            //添加操作日志
//            OperrateLogs operrateLogs= new OperrateLogs();
//            operrateLogs.setMofDivCode(userDTO.getProvince());
//            operrateLogs.setTokenId(tokenid);
//            operrateLogs.setUserAgencyId(userDTO.getAgency());
//            operrateLogs.setUeserName(userDTO.getName());
//            operrateLogs.setOperateName("财务审核--收款户配置--新增");
//            iOperrateLogsService.saveOperrateLogs(operrateLogs);

            iPayeeAcctService.deleteAccount(accountId);
            return ApiResultDTO.success();
        } catch (AppRuntimeException e) {
            logger.error("保存收款账户信息失败", e.getMessage());
            return ApiResultDTO.error(ERROR_RSCODE, "保存收款账户信息失败");
        }
    }
    /**
     * 收款户导出
     * @return
     */
    @GetMapping("/outPutPayeeReportList")
    public ApiResultDTO outPutProvinceReportList( @RequestHeader("X-Token") String tokenid,
                                                  HttpServletResponse response){
        try {
            UserDTO userDTO = getUser(tokenid);

            String fileName = "区划数据初始化";
            OutputStream outputStream = getOutputStream(fileName,response);
            iPayeeAcctService.payeeAcctsExcel(outputStream);
            return ApiResultDTO.success();
        } catch (Exception e){
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "导出失败");
        }
    }
    /**
     * 收款户配置数据导入
     * @return
     */
    @PostMapping("/payeeImportSalaryReportList")
    public ApiResultDTO provinceImportSalaryReportList(@RequestHeader("X-Token") String tokenid,
                                                       @RequestParam(value = "salaryMonth") String salaryMonth,
                                                       @RequestParam("file") MultipartFile file){
        try {
//            System.out.println("开始时间："+DateUtils.getCurrentTimeStamp());
            UserDTO userDTO = getUser(tokenid);
            String province = userDTO.getProvince();
            List<String> payeeExccel = iPayeeAcctService.getExccelColumnName();
            iPayeeAcctService.Read(file,province,salaryMonth,payeeExccel);
            return ApiResultDTO.success();
        } catch (AppRuntimeException e){
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, e.getMessage());
        } catch (Exception e) {
            logger.error(e.getMessage(), e);
            return ApiResultDTO.error(ERROR_RSCODE, "导入失败");
        }

    }




}
